package com.qlhx.common;

import com.qlhx.common.util.LoggerUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * 
 * 开发公司：SOJSON在线工具
 * <p>
 * 版权所有：© www.qlhx.com
 * <p>
 * 博客地址：http://www.sojson.com/blog/
 * <p>
 * <p>
 * 
 * Redis Manager Utils
 * 
 * <p>
 * 
 * 区分　责任人　日期　　　　说明<br/>
 * 创建　周柏成　2016年6月2日 　<br/>
 *
 * @author zhou-baicheng
 * @email so@qlhx.com
 * @version 1.0,2016年6月2日 <br/>
 * 
 */

public class JedisManager {
    private JedisPool jedisPool ;
    /**
     * flag 0:jedisPool方式<br />
     * 1:jedisSentinelPool方式
     */
    private Integer flag;

    public  JedisManager(){

    }

    public  JedisManager(JedisPool jedisPool){
        this.jedisPool = jedisPool;
    }

    public Jedis getJedis() {
	Jedis jedis = null;
	try {

		jedis = getJedisPool().getResource();

	} catch (JedisConnectionException e) {
	    String message = StringUtils.trim(e.getMessage());
	    if ("Could not get a resource from the pool"
		    .equalsIgnoreCase(message)) {
		System.out.println("++++++++++请检查你的redis服务++++++++");
		System.out
			.println("|①.请检查是否安装redis服务，如果没安装，Windos 请参考Blog：http://www.sojson.com/blog/110.html|");
		System.out
			.println("|②.请检查redis 服务是否启动。启动口诀[安装目录中的redis-server.exe，双击即可，如果有错误，请用CMD方式启动，怎么启动百度，或者加QQ群。]|");
		System.out
			.println("|③.请检查redis启动是否带配置文件启动，也就是是否有密码，是否端口有变化（默认6379）。解决方案，参考第二点。如果需要配置密码和改变端口，请修改spring-cache.xml配置。|");
		System.out
			.println("|④.QQ群：259217951，目前需要付费，免费的方案请参考链接：http://www.sojson.com/shiro");

		System.out
			.println("|PS.如果对Redis表示排斥，请使用Ehcache版本：http://www.sojson.com/jc_shiro_ssm_ehcache.html");
		System.out
			.println("项目退出中....生产环境中，请删除这些东西。我来自。JedisManage.java line:53");
		System.exit(0);// 停止项目
	    }
	    e.printStackTrace();
	    throw new JedisConnectionException(e);
	} catch (Exception e) {
	    throw new RuntimeException(e);
	}
	return jedis;
    }

    public void returnResource(Jedis jedis, boolean isBroken) {
	if (jedis == null)
	    return;
	/**
	 * @deprecated starting from Jedis 3.0 this method will not be exposed.
	 *             Resource cleanup should be done using @see
	 *             {@link Jedis#close()} if (isBroken){
	 *             getJedisPool().returnBrokenResource(jedis); }else{
	 *             getJedisPool().returnResource(jedis); }
	 */
	jedis.close();
    }

    public byte[] getValueByKey(int dbIndex, byte[] key) throws Exception {
	Jedis jedis = null;
	byte[] result = null;
	boolean isBroken = false;
	try {
	    jedis = getJedis();
	    jedis.select(dbIndex);
	    result = jedis.get(key);
	} catch (Exception e) {
	    isBroken = true;
	    throw e;
	} finally {
	    returnResource(jedis, isBroken);
	}
	return result;
    }

    public void deleteByKey(int dbIndex, byte[] key) throws Exception {
	Jedis jedis = null;
	boolean isBroken = false;
	try {
	    jedis = getJedis();
	    jedis.select(dbIndex);
	    Long result = jedis.del(key);
	    LoggerUtils.fmtDebug(getClass(), "删除Session结果：%s", result);
	} catch (Exception e) {
	    isBroken = true;
	    throw e;
	} finally {
	    returnResource(jedis, isBroken);
	}
    }

    public void saveValueByKey(int dbIndex, byte[] key, byte[] value,
	    int expireTime) throws Exception {
	Jedis jedis = null;
	boolean isBroken = false;
	try {
	    jedis = getJedis();
	    jedis.select(dbIndex);
	    jedis.set(key, value);
	    if (expireTime > 0)
		jedis.expire(key, expireTime);
	} catch (Exception e) {
	    isBroken = true;
	    throw e;
	} finally {
	    returnResource(jedis, isBroken);
	}
    }

    public JedisPool getJedisPool() {
	return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
	this.jedisPool = jedisPool;
    }
//
//    public JedisSentinelPool getJedisSentinelPool() {
//	return jedisSentinelPool;
//    }
//
//    public void setJedisSentinelPool(JedisSentinelPool jedisSentinelPool) {
//	this.jedisSentinelPool = jedisSentinelPool;
//    }

    /**
     * @return the flag
     */
    public Integer getFlag() {
	return flag;
    }

    /**
     * @param flag
     *            the flag to set
     */
    public void setFlag(Integer flag) {
	this.flag = flag;
    }

//    /**
//     * 获取所有Session
//     *
//     * @param dbIndex
//     * @param redisShiroSession
//     * @return
//     * @throws Exception
//     */
//    @SuppressWarnings("unchecked")
//    public Collection<Session> AllSession(int dbIndex, String redisShiroSession)
//	    throws Exception {
//	Jedis jedis = null;
//	boolean isBroken = false;
//	Set<Session> sessions = new HashSet<Session>();
//	try {
//	    jedis = getJedis();
//	    jedis.select(dbIndex);
//
//	    Set<byte[]> byteKeys = jedis
//		    .keys((JedisShiroSessionRepository.REDIS_SHIRO_ALL)
//			    .getBytes());
//	    if (byteKeys != null && byteKeys.size() > 0) {
//		for (byte[] bs : byteKeys) {
//		    Session obj = SerializeUtil.deserialize(jedis.get(bs),
//			    Session.class);
//		    if (obj instanceof Session) {
//			sessions.add(obj);
//		    }
//		}
//	    }
//	} catch (Exception e) {
//	    isBroken = true;
//	    throw e;
//	} finally {
//	    returnResource(jedis, isBroken);
//	}
//	return sessions;
//    }
}
